<?php

namespace app\controller;

use app\BaseController;
use think\Request;
use app\service\UserService;
use Exception;
use Firebase\JWT\JWT;

class User extends BaseController
{
    protected $middleware = [
        \app\middleware\LoginCheck::class     => ['except'     => ['login']]
    ];

    protected $request;
    protected $userService;
    public function __construct(Request $request,UserService $userService)
    {       
        $this->request = $request;
        $this->userService = $userService;
    }

    public function login()
    {
        try {
            $params = $this->request->param();
            if (empty($params['userName']) || empty($params['password'])) {
                throw new Exception('用户名或密码不能为空');
            }
            $res = $this->userService->login($params['userName'], $params['password']);
            if (!$res) {
                throw new Exception('用户名或密码错误');              
            }

            $key = "xuri";
            $payload = array(
                "iss" => "xuri",
                "aud" => "xuri",
                "iat" => time(),
                "nbf" => time(),
                "exp" => time() + 3600*24*3,
                "data" => $res
            );

            $jwt = JWT::encode($payload, $key, 'HS256');
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success($jwt);
    }    

    public function userInfo()
    {       
        try {            
            $data = $this->userService->userInfo($this->request->userInfo->id);           
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function info()
    {       
        try {            
            $data = $this->userService->info($this->request->userInfo->id);           
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function all()
    {       
        try {   
            $params = $this->request->param();                
            $data = $this->userService->all($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }   

    public function list()
    {       
        try {            
            $params = $this->request->param();           
            $data = $this->userService->list($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function save()
    {       
        try {    
            $params = $this->request->param();  
            $data = $this->userService->save($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function delete()
    {       
        try {    
            $params = $this->request->param();  
            $data = $this->userService->delete($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

   
}
